package com.urguxxi.springdemo.interceptor;

import com.sun.deploy.net.HttpResponse;
import org.springframework.http.HttpRequest;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception{
        //获取session
        HttpSession session = request.getSession();

        //获取存放的登录标记
        String adminName = (String) session.getAttribute("adminName");

        //判断session的值是否为null
        if(adminName==null){
            response.sendRedirect("/login/page");
            return false;
        }

        //注意TRUE代表放行，false代表拦截
        return false;
    }
}
